/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ipostservices.model.dao;

import ipostservices.model.bean.User;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author sixviv
 */
public class UserDAO extends GeneralDAO<User>{

    public User getByEmail(String email){
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            Criteria cri = session.createCriteria(User.class);
            cri.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
            cri.setFetchMode("projects", FetchMode.JOIN);
            cri.setFetchMode("projects_1", FetchMode.JOIN);
            cri.add(Restrictions.eq("email", email));
            return (User)cri.uniqueResult();
        }finally{
            session.close();
        }
//        List<User> users = buscar("email='"+email+"'",User.class);
//        if(!users.isEmpty())
//            return users.get(0);
//        else
//            return null;
    }

    public User getById(int idUser){
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            Criteria cri = session.createCriteria(User.class);
            cri.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
            cri.setFetchMode("projects", FetchMode.JOIN);
            cri.setFetchMode("projects_1", FetchMode.JOIN);
            cri.add(Restrictions.eq("iduser", idUser));
            return (User)cri.uniqueResult();
        }finally{
            session.close();
        }
    }
}
